Skip to content

factory-server: record the cost OpenRouter charged - #294

Merged
moonthebot merged 7 commits into
mainfrom
factory/job-138-pr-9
Nov 27, 2026
Merged

moonthebot merged 7 commits into
mainfrom
factory/job-138-pr-9

Conversation

@moonthebot

@moonthebot moonthebot commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Why

An OpenRouter run bills real money, so the job total should show what OpenRouter charged. After a run, the server reads each generation's total_cost from GET /api/v1/generation?id= with the run's key and records the sum. The lookup runs in a background task and never delays or fails the run. When a cost cannot be read, the run adds nothing.

Details:

  • A zero shows as $0.00 only when OpenRouter confirmed it (job_costs.fetched).
  • The worker receives the job that selected the task, and the charge is filed under that snapshot.
  • The scheduler registers an OpenRouter worker as a pending lookup at dispatch, checking under the store lock that the job is still in the dispatched stage. A cancel before it, or a store error, starts no worker and the next pass retries. A cancel after it keeps the page polling until the charge arrives.
  • The job page reads the job row, then the pending lookup, then the log and cost.
  • The whole lookup has a 90 second wall-clock deadline and records no partial cost on expiry.

Docs

docs/design.md: the Job cost row and the sections on scheduling and recording an OpenRouter run describe the fetched flag, the step attribution, the worker registration, polling while a lookup is pending and the deadline.

Verification

  • nix develop -c cargo test -p factory-server --features e2e-fakes: passed, including a dashboard test that loads a cancelled job page after registration, checks it polls, and checks the fragment shows the charge.
  • nix develop -c cargo clippy --workspace --all-targets --features e2e-fakes: no warnings.
  • nix develop -c just e2e: 238 passed on the previous commit; this change touches no browser code.
  • Not run: a real run against OpenRouter (criterion 21).

moonthebot and others added 7 commits November 27, 2026 16:05
After an OpenRouter run the server reads each generation's total_cost
from GET /api/v1/generation with the run's key and adds the sum through
Store::add_cost, not marked estimated. The lookup runs in a background
task with retries for about a minute, so it never fails the run or delays
the job's next step. When an id stays unreadable or the output showed
none, the run adds no cost and the log says so with the token counts.
A fetched $0 shows $0.00.

Co-authored-by: Copilot <[email protected]>
A zero from a subscription run stays a dash unless OpenRouter
confirmed it, which the job_costs row now records in `fetched`.
A late lookup files its charge under the stage and pull request of
the run, and the timeline no longer reads that entry as a step
change. A terminal job keeps polling while a lookup is pending.
The lookup has a 90 second wall-clock deadline and records nothing
when it expires. Comments that repeated the code are removed.

Co-authored-by: Copilot <[email protected]>
The job page reads the pending lookup before the log and the cost, so
a lookup that ends mid-read keeps the page polling. A run's charge is
filed under the job as it was when the run was dispatched, so a
cancellation before the report cannot move it off the step. The design
and the Local-only rule in the store now say a confirmed OpenRouter
zero shows as $0.00, and the project forge migration comment is back
above its function.

Co-authored-by: Copilot <[email protected]>
The worker receives the job that selected the task instead of reading
it again, so a cancellation after dispatch cannot move a charge off
its step, and a store error is no longer dropped. The job page reads
the job row before the pending lookup, which closes the race where a
lookup began and the job ended between the two reads.

Co-authored-by: Copilot <[email protected]>
A cancel can end the job before the worker reaches its cost lookup, so
the page rendered by the cancel redirect had no poller and missed the
late charge. The scheduler now counts the worker in cost_lookups from
dispatch until it ends, and the lookup it starts counts on its own.

Co-authored-by: Copilot <[email protected]>
A cancel while the agent request was built left a terminal page with no
poller, and the scheduler then started the worker from a stale
snapshot. The registration now checks the job's stage under the store
lock and starts no worker when it changed.

Co-authored-by: Copilot <[email protected]>
A store error while registering the lookup started the worker with no
pending marker. The scheduler now logs it and retries on the next
pass. A dashboard test loads the cancelled job page after registration
and checks it polls until the charge shows in the fragment.

Co-authored-by: Copilot <[email protected]>
@moonthebot
moonthebot merged commit ed6b926 into main Nov 27, 2026
3 checks passed
@moonthebot
moonthebot deleted the factory/job-138-pr-9 branch November 27, 2026 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant